package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.permission.AdminDto;
import com.bole.dto.permission.MenuDto;
import com.bole.feign.MenuFeign;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yakamaxi
 * @version 1.0
 * @date 2020/8/12 18:54
 */
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuFeign menuFeign;

    @GetMapping
    public Result get(){
        AdminDto admin = (AdminDto) SecurityUtils.getSubject().getPrincipal();
        return menuFeign.getMenu(admin.getId());
    }

    @GetMapping("/zTree")
    public List<Map<String, Object>> getTree(){
        Result result = menuFeign.getzTree();
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.getData();
        return list;
    }

    @PostMapping
    public Result addMenu(@RequestBody MenuDto menu){
        return menuFeign.add(menu);
    }

    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Long id , @RequestBody MenuDto menu){
        return menuFeign.updateById(id, menu);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        return menuFeign.deleteById(id);
    }

    @GetMapping("/getzTreeChecked/{rid}")
    public List<Map<String,Object>> getzTreeChecked(@PathVariable("rid") Long rid) {
        Result result = menuFeign.getzTreeChecked(rid);
        if (result.getCode().equals(200)){
            return (List<Map<String, Object>>) result.getData();
        }
        return null;
    }

}
